/**
 * https://github.com/gengfire/vue-popup-plugin
 * @type {Object}
 */

export function Tips(Vue) {
  let instances = [];
  let count = 1;
  let TipsConstructor = Vue.extend(require('./tips.vue'));
  var tips = function (options) {
    options = options || {};
    if (typeof options === 'string') {
      options = {
        tips: options
      };
    }
    const userOnHide = options.onHide;
    const id = 'Tips-' + count++;
    options.onHide = function () {
      tips.hide(id, userOnHide);
    };
    const instance = new TipsConstructor({
      data: options
    });
    instance.id = id;
    instance.vm = instance.$mount();
    document.body.appendChild(instance.vm.$el);
    instance.vm.visible = true;
    instance.dom = instance.vm.$el;
    instances.push(instance);
  };
  tips.hide = function (id, onHide) {
    for (let i = 0, len = instances.length; i < len; i++) {
      if (id === instances[i].id) {
        if (typeof onHide === 'function') onHide(instances[i]);
        instances.splice(i, 1);
        break;
      }
    }
  };
  Vue.prototype.$tips = tips;
}
